package com.kly.user.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.google.common.eventbus.AsyncEventBus;
import com.kly.enums.ParameterTypeEnum;
import com.kly.user.db.dao.UserChatterMappingDao;
import com.kly.user.db.dao.UserMsgSendTotalDao;
import com.kly.user.db.entity.UserChatterMappingEntity;
import com.kly.user.db.entity.UserMsgSendTotalEntity;
import com.kly.user.dto.PushMsgEvent;
import com.kly.user.enums.PushBizCodeEnum;
import com.kly.user.eventbus.ActionEnum;
import com.kly.user.eventbus.Event;
import com.kly.user.service.PushMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("pushMsgService")
public class PushMsgServiceImpl implements PushMsgService {

    @Resource
    AsyncEventBus asyncEventBus;
    @Resource
    UserChatterMappingDao userChatterMappingDao;
    @Resource
    UserMsgSendTotalDao userMsgSendTotalDao;

    @Override
    public Response pushMsg(PushMsgEvent pushMsgEvent) {
        if (pushMsgEvent == null || pushMsgEvent.getPushBizCodeEnum() == null) {
            return Response.error("pushBizCode should not be empty");
        }
        if (StringUtils.isEmpty(pushMsgEvent.getFromUserCode()) && StringUtils.isEmpty(pushMsgEvent.getToUserCode())) {
            return Response.error("fromUserCode or toUserCode should not be empty");
        }

        log.info("====pushMsg, pushMsgEvent:{}", JSONObject.toJSONString(pushMsgEvent));
        if (PushBizCodeEnum.M20004.equals(pushMsgEvent.getPushBizCodeEnum())) {

            final List<UserMsgSendTotalEntity> userMsgSendTotal = userMsgSendTotalDao.queryByUserCode(pushMsgEvent.getFromUserCode(), null);
            if (CollectionUtils.isEmpty(userMsgSendTotal)) {
                log.info("====pushMsg, userMsgSendTotal: 0, pushMsgEvent:{}", JSONObject.toJSONString(pushMsgEvent));
                return Response.ok();
            }

            for (UserMsgSendTotalEntity item : userMsgSendTotal) {
                String chatterUserCode = pushMsgEvent.getChatterUserCode();
                if (StringUtils.isEmpty(chatterUserCode)) {
                    final List<UserChatterMappingEntity> userChatterMappingEntities = userChatterMappingDao.queryByModelUserCode(item.getToUserCode());
                    if (!CollectionUtils.isEmpty(userChatterMappingEntities)) {
                        chatterUserCode = userChatterMappingEntities.get(0).getChatterUserCode();
                    }
                }

                Map<String, Object> params = new HashMap<>();
                params.put("type", ParameterTypeEnum.MESSAGE_FEEDBACK.getId());
                params.put("bizCode", PushBizCodeEnum.M20004.getPushBizCode());
                params.put("userCode", pushMsgEvent.getFromUserCode());
                params.put("modelUserCode", item.getToUserCode());

                Event event = Event.builder().eventActionEnum(ActionEnum.PUSH_MSG).app(pushMsgEvent.getApp()).fromUserCode(pushMsgEvent.getFromUserCode()).toUserCode(chatterUserCode).content(JSONObject.toJSONString(params)).build();
                log.info("====pushMsg, pushMsgEvent:{}, event:{}", JSONObject.toJSONString(pushMsgEvent), event);
                asyncEventBus.post(event);
            }
        }
        return Response.ok();
    }
}
